package com.laboratory.servicecustomer.realm;

import com.Laboratory.pojo.user;
import com.Laboratory.service.userService;
import com.laboratory.servicecustomer.constant.constantArgs;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @Author acok
 * @description: 自定义shiro权限/角色验证方法,构建bean对象放入bean工厂，交给spring容器管理
 * @Date 2022/12/4 20:41
 * @Version 1.0
 */
@Component
public class MyRealm extends AuthorizingRealm {

    @DubboReference(interfaceClass = userService.class,version = "1.0.0")
    private userService userService;

      /**
           * @return:AuthorizationInfo校验对象
           * @param:PrincipalCollection集合对象包装角色、权限信息
           * @description: 自定义用户角色、权限认证方法
           * @Author: Acok
           * @mbggenerated 2022/12/4
           */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(
            PrincipalCollection principalCollection) {
        String principal = principalCollection.getPrimaryPrincipal().toString();
        SimpleAuthorizationInfo info=new SimpleAuthorizationInfo();
        try{
            List<String> roleList = userService.getRoleListByName(principal);
            List<String> authorityList = userService.getAuthorityByName(principal);
            info.addRoles(roleList);
            info.addStringPermissions(authorityList);
        }catch (Exception e){
            e.printStackTrace();
            System.out.println(constantArgs.AUTHORIZATION_OBTAIN_EXCEPTION);
        }
        return info;
    }
      /**
           * @return:AuthenticationInfo校验对象
           * @param:AuthenticationToken包装当前需要验证对象信息
           * @description: 自定义用户登录权限认证方法
           * @Author: Acok
           * @mbggenerated 2022/12/4
           */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(
            AuthenticationToken authenticationToken) throws AuthenticationException {
        String principal=authenticationToken.getPrincipal().toString();
        user user = userService.selectUserByName(principal);
        if (user!=null){
            AuthenticationInfo info=new SimpleAuthenticationInfo(
                    authenticationToken.getPrincipal(),
                    user.getPassword(),
                    ByteSource.Util.bytes(user.getSalt()),
                    authenticationToken.getPrincipal().toString()
            );
            return info;
        }
        return null;
    }

    /**
    @Override
    public void clearCachedAuthorizationInfo(PrincipalCollection principals) {
        super.clearCachedAuthorizationInfo(SecurityUtils.getSubject().getPrincipals());
    }


    @Override
    public void clearCachedAuthenticationInfo(PrincipalCollection principals) {
        super.clearCachedAuthenticationInfo(SecurityUtils.getSubject().getPrincipals());
    }

    @Override
    public void clearCache(PrincipalCollection principals) {
        super.clearCache(principals);
    }*/
    public void clearAllPermissionCache(){
        getAuthorizationCache().clear();
    }
}
